define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'cookie', 'echarts', 'echarts-theme'], function ($, undefined, Backend, Table, Form, undefined, Echarts, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wu/hjdevice/index',
                    add_url: 'wu/hjdevice/add',
                    edit_url: 'wu/hjdevice/edit',
                    del_url: 'wu/hjdevice/del',
                    multi_url: 'wu/hjdevice/multi',
                    table: 'wu_hjdevice',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        //{field: 'client_id', title: __('Client_id')},
                        {field: 'deviceid', title: __('Deviceid')},
                        {field: 'name', title: __('Name'), operate: false},
                        //{field: 'wd_newvalue', title: __('Wd_newvalue'), operate:false},
                        //{field: 'sd_newvalue', title: __('Sd_newvalue'), operate:false},
                        //{field: 'yc_a_newvalue', title: __('Yc_a_newvalue'), operate:false},
                        //{field: 'yc_b_newvalue', title: __('Yc_b_newvalue'), operate:false},
                        //{field: 'sxmda_a_newvalue1', title: __('Sxmda_a_newvalue1'), operate:false},
                        //{field: 'sxmda_a_newvalue2', title: __('Sxmda_a_newvalue2'), operate:false},
                        //{field: 'sxmda_a_newvalue3', title: __('Sxmda_a_newvalue3'), operate:false},
                        //{field: 'sxmda_a_newvalue4', title: __('Sxmda_a_newvalue4'), operate:false},
                        //{field: 'sxmda_b_newvalue1', title: __('Sxmda_b_newvalue1'), operate:false},
                        //{field: 'sxmda_b_newvalue2', title: __('Sxmda_b_newvalue2'), operate:false},
                        //{field: 'sxmda_b_newvalue3', title: __('Sxmda_b_newvalue3'), operate:false},
                        //{field: 'sxmda_b_newvalue4', title: __('Sxmda_b_newvalue4'), operate:false},
                        //{field: 'sxmda_c_newvalue1', title: __('Sxmda_c_newvalue1'), operate:false},
                        //{field: 'sxmda_c_newvalue2', title: __('Sxmda_c_newvalue2'), operate:false},
                        //{field: 'sxmda_c_newvalue3', title: __('Sxmda_c_newvalue3'), operate:false},
                        //{field: 'sxmda_c_newvalue4', title: __('Sxmda_c_newvalue4'), operate:false},
                        //{field: 'sxmda_d_newvalue1', title: __('Sxmda_d_newvalue1'), operate:false},
                        //{field: 'sxmda_d_newvalue2', title: __('Sxmda_d_newvalue2'), operate:false},
                        //{field: 'sxmda_d_newvalue3', title: __('Sxmda_d_newvalue3'), operate:false},
                        //{field: 'sxmda_d_newvalue4', title: __('Sxmda_d_newvalue4'), operate:false},
                        //{field: 'sxmda_code', title: __('Sxmda_code'), operate:false},
                        //{field: 'zzycgj_code', title: __('Zzycgj_code'), operate:false},
                        {
                            field: 'isonline',
                            title: __('Isonline'),
                            operate: false,
                            formatter: Controller.api.formatter.olinestatus
                        },
                        {field: 'zxscnr', title: __('最新上傳內容'), operate: false},

                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'update_time',
                            title: __('Update_time'),
                            operate: false,
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,

                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    title: __('記錄'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-newspaper-o',
                                    url: 'wu/hjdevicelog/index'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                var myColumns = [];
                $.each(table.bootstrapTable('getHiddenColumns'), function (i, item) {
                    myColumns.push(item.field);
                });
                $.cookie('fa_wu_hjdevice', JSON.stringify(myColumns), {expires: 365, path: '/'});
            });
            if ($.cookie('fa_wu_hjdevice')) {
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_wu_hjdevice')), function (i, item) {
                    table.bootstrapTable('hideColumn', item);
                });
            }

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        cjjk: function () {
            console.log(WSdata);

            var timeindex = 3;
            setInterval(function () {
                timeindex++;
                //console.log("==" + timeindex);
                if (timeindex >= 3) {
                    //console.log("##" + timeindex);
                    var url = "/api/wu/cjjkdata";
                    Fast.api.ajax({
                        url: url,
                        loading: 0
                    }, function (data, ret) {
                        //console.log("1111");
                        WSdata = data;
                        //console.log(WSdata);
                        Controller.wdtu();
                        Controller.sdtu();
                        Controller.tiaotu();
                        Controller.zhetu1();
                        Controller.zhetu2();
                        return false;
                    });
                    timeindex = 0;
                }

            }, 1000);//1秒刷新一次

        },
        wdtu: function () {
            var myChart = Echarts.init(document.getElementById('echart1'), 'walden');
            var option = {
                tooltip: {
                    formatter: '{a} <br/>{b} : {c}%'
                },
                series: [{
                    name: 'Pressure',
                    type: 'gauge',
                    detail: {
                        formatter: '{value}'
                    },
                    data: [{
                        value: WSdata.wdvalue,
                        name: '溫度'
                    }]
                }]
            };
            myChart.setOption(option);
        },
        sdtu: function () {
            var myChart = Echarts.init(document.getElementById('echart2'), 'walden');
            var option = {
                tooltip: {
                    formatter: '{a} <br/>{b} : {c}%'
                },
                series: [{
                    name: 'Pressure',
                    type: 'gauge',
                    detail: {
                        formatter: '{value}'
                    },
                    data: [{
                        value: WSdata.sdvalue,
                        name: '濕度'
                    }]
                }]
            };
            myChart.setOption(option);
        },
        tiaotu: function () {
            var myChart = Echarts.init(document.getElementById('echart3'), 'walden');
            var posList = [
                'left', 'right', 'top', 'bottom',
                'inside',
                'insideTop', 'insideLeft', 'insideRight', 'insideBottom',
                'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
            ];

            var configParameters = {
                rotate: {
                    min: -90,
                    max: 90
                },
                align: {
                    options: {
                        left: 'left',
                        center: 'center',
                        right: 'right'
                    }
                },
                verticalAlign: {
                    options: {
                        top: 'top',
                        middle: 'middle',
                        bottom: 'bottom'
                    }
                },
                position: {
                    options: posList.reduce(function (map, pos) {
                        map[pos] = pos;
                        return map;
                    }, {})
                },
                distance: {
                    min: 0,
                    max: 100
                }
            };

            var config = {
                rotate: 90,
                align: 'left',
                verticalAlign: 'middle',
                position: 'insideBottom',
                distance: 15,
                onChange: function () {
                    var labelOption = {
                        normal: {
                            rotate: config.rotate,
                            align: config.align,
                            verticalAlign: config.verticalAlign,
                            position: config.position,
                            distance: config.distance
                        }
                    };
                    myChart.setOption({
                        series: [{
                            label: labelOption
                        }, {
                            label: labelOption
                        }, {
                            label: labelOption
                        }, {
                            label: labelOption
                        }]
                    });
                }
            };


            var labelOption = {
                show: true,
                position: config.position,
                distance: config.distance,
                align: config.align,
                verticalAlign: config.verticalAlign,
                rotate: config.rotate,
                formatter: '{c}  {name|{a}}',
                fontSize: 16,
                rich: {
                    name: {
                    }
                }
            };


            var option = {
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    data: ['A1', 'A2', 'A3']
                },
                //toolbox: {
                //    show: true,
                //    orient: 'vertical',
                //    left: 'right',
                //    top: 'center',
                //    feature: {
                //        mark: {show: true},
                //        dataView: {show: true, readOnly: false},
                //        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                //        restore: {show: true},
                //        saveAsImage: {show: true}
                //    }
                //},
                xAxis: [
                    {
                        type: 'category',
                        axisTick: {show: false},
                        data: ['循環過濾油箱 A1', '循環過濾油箱 A2', '第一集油箱 B1', '第一集油箱 B2']
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: 'A1',
                        type: 'bar',
                        barGap: 0,
                        label: labelOption,
                        emphasis: {
                            focus: 'series'
                        },
                        data: [WSdata.md4[0], WSdata.md5[0], WSdata.md6[0], WSdata.md7[0]]
                    },
                    {
                        name: 'A2',
                        type: 'bar',
                        label: labelOption,
                        emphasis: {
                            focus: 'series'
                        },
                        data: [WSdata.md4[1], WSdata.md5[1], WSdata.md6[1], WSdata.md7[1]]
                    },
                    {
                        name: 'A3',
                        type: 'bar',
                        label: labelOption,
                        emphasis: {
                            focus: 'series'
                        },
                        data: [WSdata.md4[2], WSdata.md5[2], WSdata.md6[2], WSdata.md7[2]]
                    },
                ]
            };

            console.log("md4");
            console.log(WSdata.md4);
            console.log("md5");
            console.log(WSdata.md5);
            console.log("md6");
            console.log(WSdata.md6);
            console.log("md7");
            console.log(WSdata.md7);

            myChart.setOption(option);
        },
        zhetu1: function () {
            var myChart = Echarts.init(document.getElementById('echart4'), 'walden');
            var option = {
                title: {
                    subtext: '循環過濾油箱',
                    x:'center',
                    //y: 'bottom',
                },
                tooltip: {
                    trigger: 'axis'
                },
                //legend: {
                //    data: ['L1', 'L2', 'L3']
                //},
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                //toolbox: {
                //    feature: {
                //        saveAsImage: {}
                //    }
                //},
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: WSdata.md1Xlist
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: 'L1',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.md1list1
                    },
                    {
                        name: 'L2',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.md1list2
                    },
                    {
                        name: 'L3',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.md1list3
                    },
                    {
                        name: 'L4',
                        type: 'line',
                        //stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ff0000',
                                lineStyle:{
                                    color:'#ff0000'
                                }
                            }
                        },
                        data: WSdata.md1list4
                    },
                    {
                        name: 'L5',
                        type: 'line',
                        //stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ff9900',
                                lineStyle:{
                                    color:'#ff9900'
                                }
                            }
                        },
                        data: WSdata.md1list5
                    },
                    {
                        name: 'L6',
                        type: 'line',
                        //stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ffee00',
                                lineStyle:{
                                    color:'#ffee00'
                                }
                            }
                        },
                        data: WSdata.md1list6
                    },
                ]
            };
            myChart.setOption(option);

        },
        zhetu2: function () {
            var myChart = Echarts.init(document.getElementById('echart5'), 'walden');
            var option = {
                title: {
                    subtext: '第一集油箱',
                    x:'center',
                    //y: 'bottom',
                },
                tooltip: {
                    trigger: 'axis'
                },
                //legend: {
                //    data: ['L1', 'L2', 'L3']
                //},
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                //toolbox: {
                //    feature: {
                //        saveAsImage: {}
                //    }
                //},
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: WSdata.md2Xlist
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: 'L1',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.md2list1
                    },
                    {
                        name: 'L2',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.md2list2
                    },
                    {
                        name: 'L3',
                        type: 'line',
                        //stack: '总量',
                        data: WSdata.md2list3
                    },
                    {
                        name: 'L4',
                        type: 'line',
                        //stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ff0000',
                                lineStyle:{
                                    color:'#ff0000'
                                }
                            }
                        },
                        data: WSdata.md2list4
                    },
                    {
                        name: 'L5',
                        type: 'line',
                        //stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ff9900',
                                lineStyle:{
                                    color:'#ff9900'
                                }
                            }
                        },
                        data: WSdata.md2list5
                    },
                    {
                        name: 'L6',
                        type: 'line',
                        //stack: '总量',
                        itemStyle : {
                            normal : {
                                color:'#ffee00',
                                lineStyle:{
                                    color:'#ffee00'
                                }
                            }
                        },
                        data: WSdata.md2list6
                    },
                ]
            };
            myChart.setOption(option);

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                olinestatus: function (value, row, index) {
                    if (row.isonline == 1) {
                        return [
                            '<a href="javascript:;" class="text-success" data-id= "' + row.id + '" > 在線 </a>'
                        ].join("")

                    } else {
                        return [
                            '<a href="javascript:;"  class="text-danger" data-id= "' + row.id + '" > 离線 </a>'
                        ].join("")
                    }
                }
            }
        }
    };
    return Controller;
});